package logic.ships.carrier;

import java.util.ArrayList;
import java.util.Collection;

import map.asteroids.AsteroidField;
import networking.messages.createMessages.ShipType;

import com.jme.math.Vector3f;
import com.jme.scene.Node;

import logic.common.missions.Mission;
import logic.common.missions.MissionFactory;
import logic.common.team.Team;
import logic.nodes.collision.DummyType;
import logic.ships.MissionShip;
import logic.ships.moveableShip.MovableShipProperties;
import logic.ships.resourceShip.ResourceShip;

public class Carrier extends ResourceShip implements MissionShip {
	
	private static final long serialVersionUID = 1L;
	
	private CarrierController carrierController;
	private Vector3f currentMoveLocation;
	private AsteroidField field;
	
	public Carrier(String modelPath, String title, Node model, MovableShipProperties properties, Team team) {
		this(modelPath, title, model, properties, team, DummyType.None);
	}
	
	public Carrier(String modelPath, String title, Node model, MovableShipProperties properties, Team team, 
			DummyType dummyType) {
		super(modelPath, title, model, properties, team, true, dummyType);
		
		if(dummyType == DummyType.None) createMoveForwardController();
	}
	
	@Override
	protected void applyDummyType() {
		super.applyDummyType();
		
		if(dummyType == DummyType.None) {
			carrierController = new CarrierController(this);
			addController(carrierController);
		}
	}
	
	public boolean arrived() { return carrierController.arrived(); }
	
	public void moveTo(AsteroidField field) {
		Vector3f fieldCenter = field.getProperties().getCenter();
		Vector3f dir = fieldCenter.subtract(getLocalTranslation());
		float dist = dir.length();
		
		currentMoveLocation = getLocalTranslation().add(dir.normalize().mult(dist - 150f));

		this.field = field;
		team.carrierMovesTo(field);
		
		getMoveForwardController().stop(false);
	}
	
	public Vector3f getCurrentMoveLocation() { return currentMoveLocation; }
	
	public AsteroidField getAsteroidField() { return field; }
	
	@Override
	public void destroy() {
		super.destroy();
		team.removeCarrier(this);
	}
	
	@Override
	public int getDestroyMoney() { return 500; }
	
	@Override
	public float getDefaultScale() { return 1.5f; }
	
	@Override
	protected ShipType getShipType() { return ShipType.Carrier; }
	
	@Override
	public Collection<Mission> getTargetMissions(Team team) {
		Collection<Mission> missions = new ArrayList<Mission>();
		
		Mission destroyMission = MissionFactory.createDestroyMission(team, this);
		missions.add(destroyMission);
		
		Mission droneMission = MissionFactory.createDroneHarassMission(team, this);
		missions.add(droneMission);
		
		return missions;
	}
}